#!/bin/sh

t_sync=$1

echo "time to start: $t_sync"

echo "cwd : $PWD"

echo "restoring my $HOME variable"
export HOME=~/
if [ -f $HOME/.bashrc ]; then
    source $HOME/.bashrc
fi
echo "home: $HOME"

export MY_WORKDIR=$PWD

echo "setting up ATLAS runtime environment"
export EventLoopDir=$HOME/EventLoop.r17.grid.dev

cd $EventLoopDir
source $EventLoopDir/setup.sh

if [ -f libshift.so.2.1 ]; then
    rm -f libshift.so.2.1
fi

ln -s /cvmfs/atlas.cern.ch/repo/sw/software/i686-slc5-gcc43-opt/17.0.4/sw/lcg/external/Grid/DPM/1.7.4-7sec/i686-slc5-gcc43-opt/lib/libdpm.so libshift.so.2.1

export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH

export X509_USER_PROXY=$MY_WORKDIR/$GANGA_PROXY_NAME

voms-proxy-info -all

rm -f run.log
rm -f ioperf.root

now=`date +%s`
while [[ $now -lt $t_sync ]]; do
    sleep 1
    now=`date +%s`
done

echo "event loop start: $now"

$EventLoopDir/bin/TimeDriver.exe --MUS 1 --EER 1 --inputFileMode 3 --fileList  'rfio://biet-2.nikhef.nl//export/data/atlasprd/atlas/2012-03-16/NTUP_TOP.615381._000008.root.1.129101311.0' --outputFilename output.singlefile.allscales.root 2>&1 > run.log

now=`date +%s`
echo "event loop stop: $now"

echo "moving output files to the workdir"
mv $EventLoopDir/run.log $MY_WORKDIR
mv $EventLoopDir/ioperf.root $MY_WORKDIR

cd $MY_WORKDIR
